<template>
  <div>
    <el-dialog :title="title" :visible.sync="visible" width="500px" append-to-body custom-class="import-dialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="房间">
          {{ data.premisesAddress }}
        </el-form-item>
        <el-form-item label="资金渠道">
          分账平台
        </el-form-item>
        <el-form-item label="账单服务费" prop="serviceCharge">
          <el-input v-model="form.serviceCharge" placeholder="请输入账单服务费" v-input.float="1"
                    @change="(e)=>form.serviceCharge=Number(e)"
          >
            <template slot="append">%</template>
          </el-input>
          账单收款后平台收取的服务费
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { debounce } from '@/utils'
import { editAllocation } from '@/api/bill/allocation'

export default {
  name: 'EditDialog',
  components: {},
  props: {
    title: {
      default: '账单分账'
    },
    value: {
      default: false
    },
    data: {
      default: undefined
    }
  },
  data() {
    return {
      form: {
        houseId: "", //房屋id
        serviceCharge: null //账单服务费
      },
      rules: {
        serviceCharge: [
          { required: true, message: '请输入账单服务费', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (value < 0 || value > 100) {
                callback(new Error('请输入0-100的数字'))
              } else {
                callback()
              }
            }
          }
        ]
      },
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 总条数
      total: 0,
      //列表
      list: []

    }
  },
  computed: {
    visible: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    }
  },
  watch: {
    value: {
      handler(newVal, oldVal) {
        if (newVal) {
          this.init()
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    init() {
      this.reset()
    },
    reset() {
      this.form = {
        houseId: this.data.houseId,
        serviceCharge: null
      }
      this.resetForm('form')
      this.form = this.data
    },
    submitForm: debounce(function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          editAllocation(this.form).then(res => {
            this.$message.success(res.msg)
            this.visible = false
            this.$emit('success')
          }).catch(err => {
            this.$message.error(err.msg)
          })
        }
      })
    }, 500,true),
    cancel() {
      this.reset()
      this.visible = false
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/assets/styles/dialog.scss";
</style>
